# 模块导入
from . import Borrow
from . import User
from . import Book
from . import database


# Borrow表的mapper
class BorrowMapper:
    # query
    # 根据borrow_id查找borrow项
    @staticmethod
    def query_borrow_id(borrow_id):
        return Borrow.query.get(borrow_id)

    # 查找borrow列表
    @staticmethod
    def query_all():
        return Borrow.query.filter().all()

    # 根据user_id查询borrow项
    @staticmethod
    def query_user_id(user_id):
        return Borrow.query.filter_by(user_id=user_id).all()

    # 根据username查询borrow项
    @staticmethod
    def query_username(username):
        userlist = User.query.filter_by(username=username).all()
        borrowlist = []
        for user in userlist:
            borrow = Borrow.query.filter_by(user_id=user.user_id).first()
            if borrow is None:
                break
            borrowlist.append(borrow)
        return borrowlist

    # 根据username模糊查询borrow项
    @staticmethod
    def query_username_like(keyword):
        userlist = User.query.filter(User.username.like('%'+keyword+'%')).all()
        borrowlist = []
        for user in userlist:
            borrow = Borrow.query.filter_by(user_id=user.user_id).first()
            if borrow is None:
                break
            borrowlist.append(borrow)
        return borrowlist

    # 根据book_id查询borrow项
    @staticmethod
    def query_book_id(book_id):
        return Borrow.query.filter_by(book_id=book_id).all()

    # 根据bookname查询borrow项
    @staticmethod
    def query_book_name(bookname):
        booklist = Book.query.filter_by(book_name=bookname).all()
        borrowlist = []
        for book in booklist:
            borrow = Borrow.query.filter_by(book_id=book.book_id).first()
            if borrow is None:
                break
            borrowlist.append(borrow)
        return borrowlist

    # 根据bookname模糊查询borrow项
    @staticmethod
    def query_book_name_like(keyword):
        booklist = Book.query.filter(Book.book_name.like('%'+keyword+'%')).all()
        borrowlist = []
        for book in booklist:
            borrow = Borrow.query.filter_by(book_id=book.book_id).first()
            if borrow is None:
                break
            borrowlist.append(borrow)
        return borrowlist

    # 根据user_id,book_id查询borrow项
    @staticmethod
    def query_user_id_book_id(user_id, book_id):
        return Borrow.query.filter_by(user_id=user_id, book_id=book_id).all()

    # 根据图书状态查询borrow项
    @staticmethod
    def query_books_status(books_status):
        return Borrow.query.filter_by(books_status=books_status).all()

    # insert
    # 增加一个borrow项
    @staticmethod
    def insert(user_id, book_id, borrow_time):
        borrow = Borrow(user_id=user_id, book_id=book_id, borrow_time=borrow_time)
        database.session.add(borrow)
        database.session.commit()
        return "insert success"

    # update
    # 更新归还时间
    @staticmethod
    def update_return_time(borrow_id, return_time):
        borrow = Borrow.query.get(borrow_id)
        borrow.return_time = return_time
        database.session.commit()
        return "update success"

    # 更新图书状态
    @staticmethod
    def update_books_status(borrow_id, books_status):
        borrow = Borrow.query.get(borrow_id)
        borrow.books_status = books_status
        database.session.commit()
        return "update success"

    # 更新borrow项
    @staticmethod
    def update(borrow_id, user_id, book_id, borrow_time, return_time):
        borrow = Borrow.query.get(borrow_id)
        borrow.user_id = user_id
        borrow.book_id = book_id
        borrow.borrow_time = borrow_time
        borrow.return_time = return_time
        database.session.commit()
        return "update success"

    # delete
    # 删除borrow项
    @staticmethod
    def delete(borrow_id):
        borrow = Borrow.query.get(borrow_id)
        database.session.delete(borrow)
        database.session.commit()
        return "delete success"
